﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Hosting;

namespace Restaurant.Core
{
    public class CommonHelper
    {
        #region 字段
        /// <summary>
        ///  ASP.Net 的信任级别
        /// </summary>
        private static AspNetHostingPermissionLevel? _trustLevel;
        #endregion

        /// <summary>
        /// 转换绝对路径
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string MapPath(string path)
        {
            if (HostingEnvironment.IsHosted)
                return HostingEnvironment.MapPath(path);

            var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;

            path = path.Replace("~/", "").TrimStart('/').Replace('/', '\\');
            return Path.Combine(baseDirectory, path);
        }

        /// <summary>
        /// 获取 ASP.Net 的信任级别
        /// </summary>
        /// <returns></returns>
        public static AspNetHostingPermissionLevel GetTrustLevel()
        {
            if (!_trustLevel.HasValue)
            {
                //初始化一个
                _trustLevel = AspNetHostingPermissionLevel.None;

                //遍历所有等级
                foreach (var trustLevel in new[] {
                                                    AspNetHostingPermissionLevel.Unrestricted,
                                                    AspNetHostingPermissionLevel.High,
                                                    AspNetHostingPermissionLevel.Medium,
                                                    AspNetHostingPermissionLevel.Low,
                                                    AspNetHostingPermissionLevel.Minimal
                                                })
                {
                    try
                    {
                        new AspNetHostingPermission(trustLevel).Demand();
                        _trustLevel = trustLevel;
                        break;
                    }
                    catch (System.Security.SecurityException)
                    {
                        //直到创建失败说明等级不够
                        continue;
                    }
                }
            }
            return _trustLevel.Value;
        }


        /// <summary>
        /// 检测是否为数字
        /// </summary>
        /// <param name="_string"></param>
        /// <returns></returns>
        public static bool IsNumberic(string _string)
        {
            if (string.IsNullOrEmpty(_string))
                return false;
            foreach (char c in _string)
                if (!char.IsDigit(c) && c != '.')
                    return false;  //if(c<'0' c="">'9')//最好的方法,在下面测试数据中再加一个0，然后这种方法效率会搞10毫秒左右

            return true;

        }
         
        /// <summary>
        /// 选择文件
        /// </summary>
        /// <returns></returns>
        public static string OpenFileDialog(string filter = "全部文件|*.*", string title = "选择文件")
        {
            System.Windows.Forms.OpenFileDialog dialog = new System.Windows.Forms.OpenFileDialog();
            dialog.Multiselect = false;
            dialog.Title = "选择文件";
            dialog.Filter = filter;// "Excel|*.xls";

            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                return dialog.FileName;
            else
                return null;
        }

        /// <summary>
        /// 选择文件夹
        /// </summary>
        /// <returns></returns>
        public static string FolderBrowserDialog()
        {
            System.Windows.Forms.FolderBrowserDialog openFileDialog = new System.Windows.Forms.FolderBrowserDialog();  //选择文件夹 
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)//注意，此处一定要手动引入System.Window.Forms空间，否则你如果使用默认的DialogResult会发现没有OK属性
                return openFileDialog.SelectedPath;
            else
                return null;
        }
    }

}
